var ajaxCart = {	
	overrideButtons : function()
        {
            $('.cart_quantity_input').typeWatch({highlight: true, wait: 600, captureLength: 0, callback: ajaxCart.update});
            $('.cart_delete').click(function(){                
                ajaxCart.remove($(this).attr('href'));
                return false;
            });
        },
        add: function(id_product, product_name, image_name, price, dimension, dimension_name, weight)
        {
            quantity = $('#product_quantity').val();
            weight = $('#weight').val();
            
            color_id = $('#color_bar a.current').attr('href');
            if (typeof(color_id)=='undefined') color_id = 0;                    

            color = $('#cc_'+color_id).val();
            if (typeof(color)=='undefined') color = 0;
 
            $.ajax({
                type: 'POST',
                url: Gidaff.SITE_URL + '/order/do_add_cart',
                data: {
                    'id_product' : id_product,
                    'product_name' : product_name,
                    'image_name' : image_name,
                    'quantity' : quantity,
                    'color_id' : color_id,
                    'color' : color,
                    'dimension' : dimension,
                    'dimension_name' : dimension_name,
                    'price' : price,
                    'weight' : weight
                },
                dataType:"json",
                success: function(data)                          
                {     
                    $('#cart_count').html(data);                            
                    alert('Ban da them '+quantity+' san pham vao gio hang');                                
                }
            });                 
        },
        update: function(val)
        {
            var rowid = $(this.el).attr('name');
            if (val==0)
            {
                ajaxCart.remove(rowid);
                return;
            }
            
            var exp = new RegExp("^[0-9]+$");            
            if (exp.test(val) == true)
            {
                $.ajax({
                    type: 'POST',
                    url: Gidaff.SITE_URL + '/order/do_update_cart',
                    data: {
                        'rowid' : rowid,
                        'qty' : val
                    },
                    dataType:"json",
                    success: function(data)                          
                    {     
                        $('#cart_count').html(data.cart_count);        
                        $('#st_'+rowid).html(data.subtotal); 
                        $('#cart_total').html(data.total);                                                       
                    }
                });                 
            }
        },
        remove:function(rowid)
        {            
            $.ajax({
                type: 'POST',
                url: Gidaff.SITE_URL + '/order/do_remove_cart',
                data: {
                    'rowid' : rowid
                },
                dataType:"json",
                success: function(data)                          
                {     
                    $('#cart_count').html(data.cart_count);        
                    $('#cart_total').html(data.total); 
                    $('#tr_'+rowid).fadeOut('slow');
                }
            });             
        }
};        

$(document).ready(function(){
	ajaxCart.overrideButtons();	
});